package com.game.sdk.view;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.text.TextUtils;

import com.game.sdk.YTAppService;

import com.game.sdk.domain.ChannelMessage;
import com.game.sdk.domain.OnChargerListener;

import com.game.sdk.ui.ChargeActivity;
import com.game.sdk.util.Constants;
import com.game.sdk.util.Logger;
import com.game.sdk.util.MResource;
import com.game.sdk.util.NetworkImpl;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ChargeView extends BaseView {

	private InputMethodManager im;
	private Activity activityt;
	private Context ctx;
	private GridView gv_pay_sort;
	private TextView tv_username, tv_money, tv_back;
	private ImageView iv_ingame;
	private Button btn_goto_pay;
	private GV_Adapter gv_adapter;
	private double channelmoney;
	public String checkpay = "checkpay";
	public ChannelMessage channelMsg;
	public static boolean ischarge = false; // 用来标记是否是自定义注册 调整到登录页面的
	private List<ChannelMessage> channels = null;

	public TextView tv_final_money;
	public TextView final_money;

	public ChargeView(FragmentActivity activity, OnChargerListener oncharger) {

		im = (InputMethodManager) activity
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		this.activityt = activity;
		ctx = activity.getApplicationContext();
		channelmoney = activity.getIntent().getDoubleExtra("money", 0);
		this.inflater = (LayoutInflater) ctx
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.content_view = inflater.inflate(MResource.getIdByName(ctx,
				Constants.Resouce.LAYOUT, "sdk_charge_two"), null);

		gv_pay_sort = (GridView) content_view.findViewById(MResource
				.getIdByName(ctx, Constants.Resouce.ID, "gv_pay_sort"));

		tv_back = (TextView) content_view.findViewById(MResource.getIdByName(
				ctx, Constants.Resouce.ID, "tv_back"));
		iv_ingame = (ImageView) content_view.findViewById(MResource
				.getIdByName(ctx, Constants.Resouce.ID, "iv_ingame"));
		btn_goto_pay = (Button) content_view.findViewById(MResource
				.getIdByName(ctx, Constants.Resouce.ID, "btn_goto_pay"));
		tv_money = (TextView) content_view.findViewById(MResource.getIdByName(
				ctx, Constants.Resouce.ID, "tv_money"));
		tv_username = (TextView) content_view.findViewById(MResource
				.getIdByName(ctx, Constants.Resouce.ID, "tv_username"));
		final_money = (TextView) content_view.findViewById(MResource
				.getIdByName(ctx, Constants.Resouce.ID, "final_money"));
		tv_final_money = (TextView) content_view.findViewById(MResource
				.getIdByName(ctx, Constants.Resouce.ID, "tv_final_money"));
		final_money.setVisibility(View.GONE);
		tv_final_money.setVisibility(View.GONE);
		tv_username.setText(YTAppService.userinfo.username);
		tv_money.setText(channelmoney + "元");
		gv_adapter = new GV_Adapter();
		gv_pay_sort.setAdapter(gv_adapter);
		channels = YTAppService.getPayChannel(channelmoney, "spay");
		gv_pay_sort.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// view.findViewById(R.id.img_check_pay).setVisibility(View.VISIBLE);
				checkpay = channels.get(position).type;
				channelMsg = channels.get(position);
				tryShowFinalMoney(checkpay, ChargeActivity.finalMoney,
						(int)ChargeActivity.returnMoney,ChargeActivity.type);
				gv_adapter.notifyDataSetChanged();
			}

		});

	}

	public void setBackOnlist(OnClickListener onclick) {
		tv_back.setOnClickListener(onclick);
		iv_ingame.setOnClickListener(onclick);
		btn_goto_pay.setOnClickListener(onclick);
	}

	private class GV_Adapter extends BaseAdapter {

		private LayoutInflater inflater;

		public GV_Adapter() {
			inflater = (LayoutInflater) ctx
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return channels == null ? 0 : channels.size();
		}

		@Override
		public Object getItem(int position) {

			return channels.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (null == convertView) {
				convertView = inflater.inflate(MResource.getIdByName(ctx,
						Constants.Resouce.LAYOUT, "sdk_charge_item"), null);
				View_hold_sort vh = new View_hold_sort();

				vh.img_alipay = (ImageView) convertView.findViewById(MResource
						.getIdByName(ctx, Constants.Resouce.ID, "img_alipay"));
				vh.img_check_pay = (ImageView) convertView
						.findViewById(MResource.getIdByName(ctx,
								Constants.Resouce.ID, "img_check_pay"));
				vh.tv_pay_type = (TextView) convertView.findViewById(MResource
						.getIdByName(ctx, Constants.Resouce.ID, "tv_pay_type"));

				convertView.setTag(vh);

			}
			final View_hold_sort vh = (View_hold_sort) convertView.getTag();
			ChannelMessage channel = channels.get(position);
			Resources resources = ctx.getResources();
			// Drawable btnDrawable =
			// resources.getDrawable(R.drawable.layout_bg);
			vh.tv_pay_type.setText(channel.channelDes);
			vh.img_check_pay.setVisibility(View.GONE);

			if (channel.type.equals("ptbpay")) {// 平台
				if (checkpay.equals("ptbpay")) {
					vh.img_check_pay.setVisibility(View.VISIBLE);
				}
				vh.img_alipay.setBackgroundDrawable(resources
						.getDrawable(MResource.getIdByName(ctx,
								Constants.Resouce.DRAWABLE, "sdk_ptb")));
			} else if (channel.type.equals("alipay")) {// ali
				if (checkpay.equals("alipay")) {
					vh.img_check_pay.setVisibility(View.VISIBLE);
				}
				vh.img_alipay.setBackgroundDrawable(resources
						.getDrawable(MResource.getIdByName(ctx,
								Constants.Resouce.DRAWABLE, "sdk_alipay")));
			} else if (channel.type.equals("shengpay")) {// 盛付通
				if (checkpay.equals("shengpay")) {
					vh.img_check_pay.setVisibility(View.VISIBLE);
				}
				vh.img_alipay.setBackgroundDrawable(resources
						.getDrawable(MResource.getIdByName(ctx,
								Constants.Resouce.DRAWABLE, "sdk_tclpay")));
			} else if (channel.type.equals("yeepay")) {// 易宝
				if (checkpay.equals("yeepay")) {
					vh.img_check_pay.setVisibility(View.VISIBLE);
				}
				vh.img_alipay.setBackgroundDrawable(resources
						.getDrawable(MResource.getIdByName(ctx,
								Constants.Resouce.DRAWABLE, "sdk_tclpay")));
			} else if (channel.type.equals("nowpay")) {// 现代支付
				if (checkpay.equals("nowpay")) {
					vh.img_check_pay.setVisibility(View.VISIBLE);
				}
				vh.img_alipay.setBackgroundDrawable(resources
						.getDrawable(MResource.getIdByName(ctx,
								Constants.Resouce.DRAWABLE, "sdk_wxpay")));
			} else if (channel.type.equals("gamepay")) {// 游戏币
				if (checkpay.equals("gamepay")) {
					vh.img_check_pay.setVisibility(View.VISIBLE);
				}
				vh.img_alipay.setBackgroundDrawable(resources
						.getDrawable(MResource.getIdByName(ctx,
								Constants.Resouce.DRAWABLE, "gamepay")));
			} else if (channel.type.equals("spay")) {// 威富通(微信)
				if (checkpay.equals("spay")) {
					vh.img_check_pay.setVisibility(View.VISIBLE);
				}
				vh.img_alipay.setBackgroundDrawable(resources
						.getDrawable(MResource.getIdByName(ctx,
								Constants.Resouce.DRAWABLE, "sdk_wxpay")));
			} else if (channel.type.equals("payeco")) {// 易联(银联 )
				if (checkpay.equals("payeco")) {
					vh.img_check_pay.setVisibility(View.VISIBLE);
				}
				vh.img_alipay.setBackgroundDrawable(resources
						.getDrawable(MResource.getIdByName(ctx,
								Constants.Resouce.DRAWABLE, "sdk_tclpay")));
			} else if (channel.type.equals("heepay")) {// 汇付宝（银联）
				if (checkpay.equals("heepay")) {
					vh.img_check_pay.setVisibility(View.VISIBLE);
				}
				vh.img_alipay.setBackgroundDrawable(resources
						.getDrawable(MResource.getIdByName(ctx,
								Constants.Resouce.DRAWABLE, "sdk_wxpay")));
			} else if (channel.type.equals("tclpay")) {// 汇付宝（银联）
				if (checkpay.equals("tclpay")) {
					vh.img_check_pay.setVisibility(View.VISIBLE);
				}
				vh.img_alipay.setBackgroundDrawable(resources
						.getDrawable(MResource.getIdByName(ctx,
								Constants.Resouce.DRAWABLE, "sdk_tclpay")));
			} else if (channel.type.equals("unionpay")) {// 汇付宝（银联）
				if (checkpay.equals("unionpay")) {
					vh.img_check_pay.setVisibility(View.VISIBLE);
				}
				vh.img_alipay.setBackgroundDrawable(resources
						.getDrawable(MResource.getIdByName(ctx,
								Constants.Resouce.DRAWABLE, "sdk_tclpay")));
			} else if (channel.type.equals("shenzhoufu")) {// 汇付宝（银联）
				if (checkpay.equals("shenzhoufu")) {
					vh.img_check_pay.setVisibility(View.VISIBLE);
				}
				vh.img_alipay.setBackgroundDrawable(resources
						.getDrawable(MResource.getIdByName(ctx,
								Constants.Resouce.DRAWABLE, "sdk_paycard")));
			} else if (channel.type.equals("xqtpay")) {// 汇付宝（银联）
				if (checkpay.equals("xqtpay")) {
					vh.img_check_pay.setVisibility(View.VISIBLE);
				}
				vh.img_alipay.setBackgroundDrawable(resources
						.getDrawable(MResource.getIdByName(ctx,
								Constants.Resouce.DRAWABLE, "sdk_wxpay")));
			} else if (channel.type.equals("xqtpayqq")) {// 汇付宝（银联）
			if (checkpay.equals("xqtpayqq")) {
				vh.img_check_pay.setVisibility(View.VISIBLE);
			}
			vh.img_alipay.setBackgroundDrawable(resources
					.getDrawable(MResource.getIdByName(ctx,
							Constants.Resouce.DRAWABLE, "sdk_qqpay")));
		}

			return convertView;
		}

	}

	static class View_hold_sort {
		private ImageView img_alipay, img_check_pay;
		private TextView tv_pay_type;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	public void tryShowFinalMoney(String type, double finalMoney,
			int returnMoney, int t) {
		// 边界值的判断
		if (TextUtils.isEmpty(type) || !type.contains("pay")
				|| finalMoney <= 0) {
			return;
		}
		if ("ptbpay".equals(type) || "gamepay".equals(type)) {
			tv_final_money.setVisibility(View.GONE);
			final_money.setVisibility(View.GONE);
			return;
		} else {
			if ((finalMoney > 0 &&t == 1)|| (returnMoney > 0&&t == 2)) {
				tv_final_money.setVisibility(View.VISIBLE);
				final_money.setVisibility(View.VISIBLE);
			}
			if (t == 1) {
				final_money.setText("" + finalMoney + "元");
			}
			if (t == 2) {
				tv_final_money.setText("充值返利");
				final_money.setText("" + returnMoney +ChargeActivity.payway);
			}
		}
	}
	@Override
	public View getContentView() {
		// TODO Auto-generated method stub

		return content_view;
	}

}
